class Person:
    def say(self):
        print("Hello, I am a person. This is the say function.")

class Teacher(Person):
    def teach(self):
        self.say()
        print("Math")

class Student(Person):
    def study(self):
        self.say()
        print("Study")

qtx = Teacher()
qtx.say()
qtx.teach()

xm = Student()
xm.say()
xm.study()

print(isinstance(qtx, Teacher))
print(isinstance(xm, Person))
print(isinstance(qtx, Student))
print(isinstance(xm, Student))

print(issubclass(Teacher, Teacher))
print(issubclass(Teacher, Person))
print(issubclass(Student, Person))
print(issubclass(Student, Student))
print(issubclass(Student, Teacher))

print(type(qtx) == Teacher)
print(type(qtx) == Person)

"""
End at P14 练习4.2024年11月23日
"""

class Animal:
    def __init__(self, name):
        self.name = name
    def eat(self):
        print(f"{self.name} is eating.")

class Dog(Animal):
    pass

class Bird(Animal):
    pass

dog = Dog("Dog")
bird = Bird("Bird")
dog.eat()
bird.eat()

print(isinstance(dog, Animal))
print(issubclass(Bird, Animal))

class Vehicle():
    def __init__(self, brand, speed):
        self.brand = brand
        self.speed = speed

class Ecar(Vehicle):
    def __init__(self, brand, speed, battery, power):
        super().__init__(brand, speed)
        self.battery = battery
        self.power = power


class A:
    def func01(self):
        print("A")
        super().func01()

class B:
    def func01(self):
      print("B")

class C(A, B):
    def func01(self):
        print("C")
        super().func01()
class D(A, B):
    def func01(self):
        print("D")
        super().func01()

class E(C, D):
    def func01(self):
        print("E")
        super().func01()
e = E()
e.func01()

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __str__(self):
        return f"{self.name} is {self.age} years old."

p = Person("Tom", 18)
print(p)

class Commodity:
    def __init__(self, cid=0, name="", price=0):
        self.cid = cid
        self.name = name
        self.price = price

    def __str__(self):
        return f"{self.name} 的编号是{self.cid}，单价是 {self.price} 元."
class Enemy:
    def __init__(self, name="", atk=0, hp=0):
        self.name = name
        self.atk = atk
        self.hp = hp

    def __str__(self):
        return f"{self.name} 的攻击力是{self.atk}，血量是 {self.hp}."

class Color:
    def __init__(self,r,g,b):
        self.r = r
        self.g = g
        self.b = b
    def __add__(self, other):
        return Color(
            self.r + other.r if self.r + other.r < 255 else 255,
            self.g + other.g if self.g + other.g < 255 else 255,
            self.b + other.b if self.b + other.b < 255 else 0)


c1 = Color(100, 100, 100)
c2 = Color(200, 200, 200)
newc = c1 + c2
print(newc.__dict__)

class Thing:
    def __init__(self, name="",hp=0):
        self.name = name
        self.hp = hp
    def __str__(self):
        return f"{self.name} 的hp是{self.mass}."

    def beaten(self, hurt):
        self.hp -= hurt
        if self.hp <= 0:
            print(f"{self.name} 已经死亡.")
        else:
            print(f"{self.name} 剩余hp是{self.hp}.")

class Shape:
    def __init__(self, name="", area=0):
        self.name = name
        self.area = area
    def __str__(self):
        return f"{self.name} 的面积是{self.area}."

class ShapeManager:
    def __init__(self):
        self.shapes = []
    def add_shape(self, shapes):
        for shape in shapes:
            self.shapes.append(shape)
    def total_area(self):
        return sum([shape.area for shape in self.shapes])

class rectangle(Shape):
    def __init__(self, name="", width=0, height=0):
        super().__init__(name, width * height)
        self.width = width
        self.height = height
    def __str__(self):
        return f"{self.name} 的面积是{self.area}."

class triangle(Shape):
    def __init__(self, name="", base=0, height=0):
        super().__init__(name, base * height / 2)
        self.base = base
        self.height = height
    def __str__(self):
        return f"{self.name} 的面积是{self.area}."

class circle(Shape):
    def __init__(self, name="", radius=0):
        super().__init__(name, radius * radius * 3.14)
        self.radius = radius
    def __str__(self):
        return f"{self.name} 的面积是{self.area}."

rec = rectangle(width=10, height=20)
tri = triangle(base=10, height=20)
cir = circle(radius=10)
sm = ShapeManager()
sm.add_shape([rec, tri, cir])
print(sm.shapes)
print(sm.total_area())